package com.health.task;

import cn.hutool.core.collection.CollStreamUtil;
import com.health.entity.Department;
import com.health.entity.Doctor;
import com.health.entity.Schedule;
import com.health.entity.Time;
import com.health.mapper.DepartmentMapper;
import com.health.mapper.DoctorMapper;
import com.health.mapper.ScheduleMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
@Slf4j
/** 定时 排班 */
public class ScheduleTask {
    @Autowired
    private ScheduleMapper scheduleMapper;
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private DoctorMapper doctorMapper;


    //每月的1号的0点
    @Scheduled(cron = "0 0 0 1 * ? ")
    public void schedule() {
        //1.获得本月的日期列表
        //1.1获取当前时间
        LocalDate now = LocalDate.now();
        //1，2获取本月的第一天
        LocalDate first = now.with(TemporalAdjusters.firstDayOfMonth());
        //1.3计算当月有多少天
        int days = now.lengthOfMonth();
        //1.4创建一个日期列表集合
        List<LocalDate> list = new ArrayList<>();
        List<Schedule> scheduleList = new ArrayList<>();
        //1.5.循环获得每天的信息
        for (int i = 0; i < days; i++) {
            LocalDate date = first.plusDays(i);
            //2.获取时段列表
            List<Time> times = scheduleMapper.getTime();
            //3.查出所有科室
            List<Department> departments = departmentMapper.getAll();
            List<Long> deptIds = departments.stream().map(Department::getId).collect(Collectors.toList());
            for (Long deptId : deptIds) {
                for (Time time : times) {
                    Schedule schedule = new Schedule();
                    //把时段，上午和下午，科室id 塞进去
                    schedule.setTime(time.getTime());
                    schedule.setType(time.getType());
                    //
                    schedule.setDeptId(deptId);
                }


//                    BeanUtils.copyProperties(department, schedule)
                //根据部门id列表去查 医生表 获取医生的id
//                List<Doctor> doctors = doctorMapper.selectAll(deptIds);
//                Map<Long, List<Doctor>> map = CollStreamUtil.groupByKey(doctors, Doctor::getId);
//                for (Doctor department : doctors) {
//                    schedule.setDate(date.toString());
//                    schedule.setType(1);
//                    schedule.setDeptId(department.getId());

            }
        }

    }
    //根据科室id date time 去插入
}